home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / ch_hash.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  2.8 KB  |  125 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  ch_hash.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_CH_HASHING_H
  16. #define LEDA_CH_HASHING_H
  17.  
  18. //------------------------------------------------------------------------------
  19. // Hashing with Chaining
  20. //
  21. // S. Naeher  (1994)
  22. //
  23. //------------------------------------------------------------------------------
  24.  
  25. #include <LEDA/basic.h>
  26.  
  27.  
  28. class ch_hash_elem 
  29. {
  30.   friend class ch_hash;
  31.  
  32.   ch_hash_elem* succ;
  33.   GenPtr k;
  34.   GenPtr i;
  35.  
  36. public:
  37.   ch_hash_elem(GenPtr key, GenPtr inf, ch_hash_elem* next = 0) 
  38.   { k = key; 
  39.     i = inf; 
  40.     succ = next;
  41.    }
  42.  
  43.   ch_hash_elem() {}
  44.  
  45.   LEDA_MEMORY(ch_hash_elem)
  46.  
  47. };
  48.  
  49. typedef ch_hash_elem*  ch_hash_item;
  50.  
  51.  
  52. //--------------------------------------------------------------------
  53. // class ch_hash
  54. //--------------------------------------------------------------------
  55.  
  56. class ch_hash 
  57. {
  58.  
  59.    static ch_hash_elem STOP;
  60.  
  61.    ch_hash_elem* table;
  62.  
  63.    int table_size;           
  64.    int table_size_1;           // table_size - 1
  65.    int low_table;           
  66.    int high_table;           
  67.    int count;
  68.  
  69.  
  70.    virtual int hash_fct(GenPtr x)      const { return long(x); }
  71.    virtual int cmp(GenPtr x, GenPtr y) const { return compare(x,y); }
  72.    virtual void clear_key(GenPtr&)  const { }
  73.    virtual void clear_inf(GenPtr&)  const { }
  74.    virtual void copy_key(GenPtr&)   const { }
  75.    virtual void copy_inf(GenPtr&)   const { }
  76.    virtual void print_key(GenPtr)   const { }
  77.  
  78.    virtual int_type() const { return 0; }
  79.  
  80.    int  next_pow(int) const;
  81.    void init(int);
  82.    void rehash(int);
  83.    void destroy();
  84.    ch_hash_item search(GenPtr,ch_hash_item&) const;
  85.  
  86.    protected:
  87.  
  88.    ch_hash_item item(GenPtr p) const { return ch_hash_item(p) ; }
  89.  
  90.    public:
  91.  
  92.    ch_hash_item lookup(GenPtr x) const;
  93.  
  94.    ch_hash_item insert(GenPtr,GenPtr);
  95.  
  96.    ch_hash_item first_item() const { return 0; }
  97.    ch_hash_item next_item(ch_hash_item) const { return 0; }
  98.  
  99.    void del(GenPtr);
  100.    void del_item(ch_hash_item);
  101.  
  102.    bool member(GenPtr x)   const  { return ( lookup(x) ? true : false ); } 
  103.  
  104.    GenPtr  key(ch_hash_item p)  const { return p->k; }
  105.    GenPtr  inf(ch_hash_item p)  const { return p->i; }
  106.    GenPtr& info(ch_hash_item p)       { return p->i; }
  107.  
  108.    void change_inf(ch_hash_item, GenPtr);
  109.    bool empty() const     { return count ? false : true ; } 
  110.    int  size()  const     { return count; } 
  111.    int  tablesize() const { return table_size ; }
  112.    void clear()           { destroy(); init(table_size);}
  113.  
  114.    ch_hash& operator=(const ch_hash&);
  115.    ch_hash(const ch_hash&);
  116.  
  117.    ch_hash(int ts = 1024) { init(next_pow(ts)); }
  118.    virtual ~ch_hash()     { destroy(); }
  119.  
  120. };
  121.  
  122.  
  123. #endif
  124.